package com.nulstudio.sppm.Controller;

import com.alibaba.fastjson2.JSONObject;
import com.nulstudio.sppm.Annotation.NormalJwt;
import com.nulstudio.sppm.Entities.CreateBasicInfoResponse;
import com.nulstudio.sppm.Entities.Commodity;
import com.nulstudio.sppm.Service.CommodityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Controller
@RestController
@CrossOrigin(value = "*")
@RequestMapping("/service/commodity")
public class CommodityController {
    @Autowired
    private CommodityService customerService;

    @NormalJwt
    @PostMapping(value = "/fetchAll",produces = "application/json;charset=UTF-8")
    public String fetchAll() {
        JSONObject json = new JSONObject();
        List<Commodity> customerList = customerService.getAllCommodities();
        json.put("code",100);
        json.put("message", "");
        json.put("version",1);
        json.put("result", customerList);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/createEntry",produces = "application/json;charset=UTF-8")
    public String createEntry(@RequestBody Map<String, Object> map) {
        JSONObject json = new JSONObject();
        String[] properties = new String[]{"name", "wholesalePrice", "retailPrice", "costPrice", "notes"};
        for (String property : properties) {
            if (!map.containsKey(property)) {
                json.put("code", 400);
                json.put("message", property + " missed");
                json.put("version", 1);
                return json.toJSONString();
            }
        }
        Commodity customerInfo = new Commodity();
        customerInfo.setName((String) map.get(properties[0]));
        customerInfo.setWholesalePrice(((Double) map.get(properties[1])).floatValue());
        customerInfo.setRetailPrice(((Double) map.get(properties[2])).floatValue());
        customerInfo.setCostPrice(((Double) map.get(properties[3])).floatValue());
        customerInfo.setNotes((String) map.get(properties[4]));
        customerService.addCommodity(customerInfo);

        final int customerId = customerService.getLastId();

        json.put("code", 0);
        json.put("message", "");
        json.put("version", 1);
        json.put("result", new CreateBasicInfoResponse(customerId));
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/modifyEntry",produces = "application/json;charset=UTF-8")
    public String modifyEntry(@RequestBody Commodity request) {
        JSONObject json = new JSONObject();
        if (request != null) {
            customerService.modifyCommodity(request);
            json.put("code", 0);
            json.put("message", "");
            json.put("version", 1);
            return json.toJSONString();
        }
        json.put("code", 401);
        json.put("message", "no modify request received");
        json.put("version", 1);
        return json.toJSONString();
    }

    @NormalJwt
    @PostMapping(value ="/deleteEntry",produces = "application/json;charset=UTF-8")
    public String deleteEntry(@RequestBody Map<String, Object> map){
        JSONObject json = new JSONObject();
        final ArrayList<Integer> id = (ArrayList<Integer>) map.get("id");
        for(int i : id) {
            customerService.deleteCommodity(String.valueOf(i));
        }
        json.put("code", 0);
        json.put("message", "");
        json.put("result", "");
        json.put("version", 1);
        return json.toJSONString();
    }
}
